package com.arcvideo.acsconnect.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.arcvideo.tyingyitonginhouse.R;

import butterknife.ButterKnife;

/**
 * @author shenmengchao
 * @version 1.0.0
 * @date 2017/7/28
 * @description
 */

public class AppDownloadDialog extends Dialog {

    private Context mContext;
    private Activity mActivity;

    public AppDownloadDialog(@NonNull Activity activity) {
        super(activity, R.style.Dialog);
        this.mContext = activity;
        this.mActivity = activity;
        init();
//        initView();
    }


    private void init() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.layout_dialog_app_download, null, false);
        ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        ButterKnife.bind(this, view);
        addContentView(view, layoutParams);

        Window window = this.getWindow();
        WindowManager m = window.getWindowManager();
        Display d = m.getDefaultDisplay();
        WindowManager.LayoutParams p = window.getAttributes();
        p.width = (int) (d.getWidth() * 0.22);
        p.height = (int) (d.getHeight() * 0.50);
        window.setAttributes(p);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_RIGHT:
            case KeyEvent.KEYCODE_DPAD_DOWN:
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                dismiss();
                break;

            case KeyEvent.KEYCODE_DPAD_LEFT:
            case KeyEvent.KEYCODE_DPAD_UP:
            case KeyEvent.KEYCODE_VOLUME_UP:
                dismiss();
                break;

            case KeyEvent.KEYCODE_ENTER:     //确定键enter
            case KeyEvent.KEYCODE_DPAD_CENTER:
                dismiss();
                break;
            case KeyEvent.KEYCODE_BACK:
                dismiss();
                break;
            default:
                break;
        }

        return super.onKeyDown(keyCode, event);
    }
}
